package it.CookBook;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	ArrayList<NodeBean> listFiles = new ArrayList<NodeBean>();
	
	Gallery gallery;
	String cookBookRoot = "/mnt/sdcard/Ricettario";
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    gallery = (Gallery) findViewById(R.id.gallery);
	    gallery.setAdapter(new ImageAdapter(this,cookBookRoot));

	    gallery.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//	            Animation grow = AnimationUtils.loadAnimation(MainActivity.this, R.anim.grow);
//
//	            View sideView = parent.findViewById(position - 1);
//	            if (sideView != null)
//	               ((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(450, 400));
//
//	            sideView = parent.findViewById(position + 1);
//	            if (sideView != null)
//	               ((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(450, 400));
//
//	            v.startAnimation(grow);
//	            v.setLayoutParams(new Gallery.LayoutParams(450, 400));
//                ImageView imageView = (ImageView) findViewById(R.id.image1);                
//                imageView.setImageResource(mImageIds[position]);
	        	NodeBean nodeBean = ((NodeBean)listFiles.get(position));
	        	if (nodeBean.getFile().getAbsolutePath().equals(cookBookRoot)){
	        		Toast.makeText(MainActivity.this, "Non puoi andare oltre!!!", Toast.LENGTH_SHORT).show();
	        		return;
	        	}
	        	
	        	if (((NodeBean)listFiles.get(position)).getFile().getName().contains(".jpg")){
		            Intent intent = new Intent(MainActivity.this, ShowImage.class);
		            Bundle b = new Bundle();
		            b.putString("imageName", nodeBean.getFile().getAbsolutePath()); //Your id
		            intent.putExtras(b);
		            startActivity(intent);
	    		}
	        	else{
	        		if (nodeBean.getName() == "UP")
	        			nodeBean.setFile(nodeBean.getFile().getParentFile());
	        		
		    	    gallery = (Gallery) findViewById(R.id.gallery);
		    	    gallery.setAdapter(new ImageAdapter(parent.getContext(),nodeBean.getFile().getAbsolutePath()));
		    	    gallery.invalidate();
	        	}
	            Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
	        }
	    });
	}

	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;

	    public ImageAdapter(Context c, String root) {
	        mContext = c;
	        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
	        mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
	        attr.recycle();
//			ContextWrapper cw = new ContextWrapper(getApplicationContext());
//			File directory1 = cw.getDir(root, Context.MODE_PRIVATE);
	        File directory = new File(root);
	        listFiles.clear();
			CookBookExplorer.createNodes(directory, new ArrayList<String>(), listFiles, root.equals(cookBookRoot));	        
	    }

	    public int getCount() {
	        return listFiles.size();
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {

//	    	 View cell = convertView;
//	    	 if (cell == null) {                         
//    	         LayoutInflater inflater = getLayoutInflater();
//    	         cell = inflater.inflate(R.layout.cell, parent, false);
//	    	 }
//	    	 TextView label=(TextView)cell.findViewById(R.id.label);
//	    	 label.setText("Prova");
//	    	ImageView imageView=(ImageView)cell.findViewById(R.id.image);
	    	
			
	    	ImageView imageView = new ImageView(mContext);
	    	imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	    	imageView.setLayoutParams(new Gallery.LayoutParams(300, 250));
	    	imageView.setPadding(50, 50, 50, 50);
	    	
			if (((NodeBean)listFiles.get(position)).getFile().isDirectory()){
		        imageView.setBackgroundResource(mGalleryItemBackground);

				Bitmap viewBitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
				Canvas canvas = new Canvas(viewBitmap);
				Paint paint = new Paint(); 
				paint.setColor(Color.BLACK); 
				paint.setStyle(Style.FILL);
				canvas.drawPaint(paint); 
				paint.setColor(Color.WHITE); 
				paint.setTextSize(20); 
				canvas.drawText(((NodeBean)listFiles.get(position)).getFile().getName(), 10, 25, paint);
				imageView.setImageBitmap(viewBitmap);
			}
			else{
				//imageView.setImageURI(Uri.fromFile(((NodeBean)listFiles.get(position)).getFile()));
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inTempStorage = new byte[16*1024];
				options.inSampleSize = 4;
				Bitmap bmp = BitmapFactory.decodeFile(((NodeBean)listFiles.get(position)).getFile().getAbsolutePath(), options);
				imageView.setImageBitmap(bmp);
				//bmp.recycle();
			}
			
	        return imageView;
	    }
	}
}
